package com.nami.web.controller.conver;

import com.nami.store.api.request.conversation.ConverAddReq;
import com.nami.store.api.request.conversation.ConverItem;
import com.nami.store.api.service.RemoteConverService;
import com.nami.store.api.service.RemoteUserService;
import com.nami.store.api.response.user.UserDTO;
import com.nami.store.api.response.conversation.ConverDetailVO;
import com.nami.web.dto.conver.ConverDTO;
import com.nami.web.facade.ConverFacadeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 会话
 *
 * @author lbc
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/conver")
public class ConverController {

    @DubboReference
    RemoteConverService converService;

    @DubboReference
    RemoteUserService userService;

    private final ConverFacadeService converFacadeService;

    @GetMapping("/list")
    public TableDataInfo<ConverDetailVO> convers(ConverDTO converDTO) {
        return converFacadeService.getConver(converDTO);
    }

    /**
     * @param
     * @return
     */
    @GetMapping("/genRandom")
    public R<Boolean> random() {

        List<String> query = new ArrayList<>();
        for (int i = 0; i < 3001; i++) {
            query.add(i + "");
        }

        Map<String, UserDTO> map = userService.getUsersByUserIds(query);

        List<ConverAddReq> addReqs = new ArrayList<>();
        ConverAddReq req = null;
        for (int i = 1; i < 3000; i++) {

            req = new ConverAddReq();

            req.setUserId(i + "");
            req.setLatestUnreadMsgIndex(0);
            req.setLatestReadMsgIndex(0);

            req.setAppKey("test");
            req.setTargetId("12345678");
            req.setConverType(2);
            req.setSyncTime(System.currentTimeMillis());
            req.setSortTime(System.currentTimeMillis());

            req.setDelFlag("0");
            req.setCreateTime(new Date());

            addReqs.add(req);
        }
        converService.batchSave(addReqs);
        return R.ok();
    }


    /**
     * @param
     * @return
     */
    @GetMapping("/test")
    public R<Boolean> updTest() {

        List<ConverItem> items = new ArrayList<>();
        ConverItem item = new ConverItem();
        item.setAppKey("test");
        item.setUserId("2");
        item.setTargetId("12345678");
        item.setConverType(2);
        item.setSyncTime(System.currentTimeMillis());
        item.setSortTime(System.currentTimeMillis());
        item.setLatestUnreadMsgIndex(1);
        item.setLatestMsgId("1233t4343");

        items.add(item);

        item = new ConverItem();
        item.setAppKey("test");
        item.setUserId("3");
        item.setTargetId("12345678");
        item.setConverType(2);
        item.setSyncTime(System.currentTimeMillis());
        item.setSortTime(System.currentTimeMillis());
        item.setLatestUnreadMsgIndex(1);
        item.setLatestMsgId("1233t4343");

        items.add(item);

        item = new ConverItem();
        item.setAppKey("test");
        item.setUserId("4");
        item.setTargetId("12345678");
        item.setConverType(2);

        item.setSyncTime(System.currentTimeMillis());
        item.setSortTime(System.currentTimeMillis());
        item.setLatestUnreadMsgIndex(1);
        item.setLatestMsgId("1233t4343");


        items.add(item);

        converService.updConvers(items);
        return R.ok();
    }


}
